#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

__author__ = "Yunpeng Li <ypli.chn@outlook.com.com>"

from EU.models import News
import json
def get_news_list(start,num=4):
    """
    获取新闻列表
    :param start: 启始索引
    :param num: 新闻个数
    :return: 新闻数组
    """
    start = int(start)
    num = int(num)

    result = []
    news_list = News.objects.all().order_by('-id')[start:start+num]
    # print("news_list:%s" % news_list)
    for news in news_list:
        result.append({
            "id":news.id,
            "title":news.title,
            "author":news.author,
            "date":news.date.timestamp()#.strftime('%Y-%m-%d')
                       })
    return json.dumps(result)

def get_news(id):
    """
    获取新闻
    :param id: 新闻id
    :return: 新闻信息HTML
    """
    result={}
    news=News.objects.get(id=id)
    result={ "id":news.id,
                "title":news.title,
                "date":news.date.strftime('%Y-%m-%d'),
                "author":news.author,
                "content":news.content
             }
    return result


def main():
    pass

if __name__ == '__main__':
    main()
